# Find the Unix so dependencies of dakota, excluding system libraries, 
# and install to ${CMAKE_INSTALL_PREFIX}/bin

# NOTE: This script will only work for make install from top of build tree
# TODO: Review string quoting conventions and test with spaces in filename


execute_process(COMMAND chrpath -v
                OUTPUT_QUIET
                ERROR_QUIET
                RESULT_VARIABLE chrpath_exists_return)

if(chrpath_exists_return EQUAL 0)
  set(CHANGE_RPATH True)
else()
  set(CHANGE_RPATH False)
  message(WARNING "Utility chrpath could not be run. Portability of package/install may be reduced.")
endif()

# Function to install a single Dakota dll dependency
# (used by multiple platforms)
function(dakota_install_dll dakota_dll)
  if (EXISTS "${dakota_dll}")
    get_filename_component(dll_filename "${dakota_dll}" NAME)
    message("-- Installing: ${CMAKE_INSTALL_PREFIX}/bin/${dll_filename}")
    execute_process(COMMAND 
	    ${CMAKE_COMMAND} -E copy "${dakota_dll}" "${CMAKE_INSTALL_PREFIX}/bin")

    #    file(COPY "${dakota_dll}" DESTINATION "${CMAKE_INSTALL_PREFIX}/bin" FILE_PERMISSIONS
    #  OWNER_READ OWNER_WRITE OWNER_EXECUTE GROUP_READ GROUP_EXECUTE WORLD_READ WORLD_EXECUTE) 
    if(CHANGE_RPATH)
      execute_process(COMMAND
          chrpath -r '\$ORIGIN:\$ORIGIN/../lib:\$ORIGIN/../bin' ${CMAKE_INSTALL_PREFIX}/bin/${dll_filename}
          OUTPUT_QUIET
          RESULT_VARIABLE chrpath_return
      )
      if(NOT chrpath_return EQUAL 0)
        message(WARNING "chrpath setting for ${dll_filename} failed; portability of install/package may be reduced")
      endif()
    endif()
  else()
    message(WARNING "Install couldn't find dynamic dependency ${dakota_dll}")
  endif()
endfunction()

message( "CMAKE_CURRENT_BINARY_DIR (1): ${CMAKE_CURRENT_BINARY_DIR}" ) 
if ( DAKOTA_JENKINS_BUILD OR DEFINED ENV{WORKSPACE} )
  # By convention, all Dakota, jenkins-driven build jobs use a 'build'
  # subdir for clear separation of source and build trees in the WORKSPACE
  set( CMAKE_CURRENT_BINARY_DIR $ENV{WORKSPACE}/build )
elseif ( NOT CMAKE_CURRENT_BINARY_DIR )
  set( CMAKE_CURRENT_BINARY_DIR $ENV{PWD} )
endif()
message( "CMAKE_CURRENT_BINARY_DIR (2): ${CMAKE_CURRENT_BINARY_DIR}" ) 

# ldd may resolve symlinks, do the same for the build tree location
get_filename_component(resolved_build_dir ${CMAKE_CURRENT_BINARY_DIR} REALPATH)

# Get the shared objects excluding system libraries and anything in
# the build tree (as will be installed to lib/) as a
# semicolon-separated list
execute_process(
  COMMAND ldd "${CMAKE_CURRENT_BINARY_DIR}/src/dakota"
  COMMAND awk "/=>/ {print $3}"
  # Omit linux-vdso.so and ld-linux-x86-64.so for example
  COMMAND grep ".so"
  # Omit libs in the build tree
  COMMAND egrep -v "${CMAKE_CURRENT_BINARY_DIR}/.+.so"
  COMMAND egrep -v "${resolved_build_dir}/.+.so"
  # Omit other system libraries
  COMMAND egrep -v "(^/lib|^/usr/lib)"
  COMMAND tr "\\n" ";"
  OUTPUT_VARIABLE dakota_unix_sos
  )

# NOTE: The list will have a trailing empty item due to trailing semicolon,
# but it's guarded against below; set policy to accept empty
cmake_policy(PUSH)
cmake_policy(SET CMP0007 NEW)
# Guard against duplicates
list(REMOVE_DUPLICATES dakota_unix_sos)
cmake_policy(POP)

# Install each DLL, guarding against empty strings
foreach(dakota_dll ${dakota_unix_sos})
  if(dakota_dll)
    dakota_install_dll("${dakota_dll}")
  endif()
endforeach()
